Welcome back. A function can be defined so that it does different things,
depending on parameter values that are passed to it.
For example, here's a definition of the function Hello2,
the only difference in this function versus
our previous Hello is that we have another variable name inside the parentheses.
We've called it S, that's called a formal parameter name,
any variable name inside those parentheses.
So, sometimes we call it a formal parameter or a parameter name.
Then, when we invoke the function,
and on line five,
we'll pass some string into it.
In this case, we're going to pass the string Iman.
By the way that's Iman [inaudible] he's
the guy who developed a practice tool that I hope you are using a lot.
When we do that invocation,
the variable s gets the value Iman at the beginning of the execution of the function.
It's like having a behind the scenes assignment statement.
It's as if we have a line of code that says s equals Iman.
Then, in the rest of the execution,
we'll be able to refer to the variable s. So,
let's actually do this using CodeLens.
We first create the function and assign it to the name Hello2,
and then we get to line five,
we're going to invoke the function.
But behind the scenes, there was this assignment,
s is now bound to Iman.
So that when we print Hello plus s,
we get Hello Iman in the output window,
and then it says "Glad to meet you" and we're done with that invocation.
The second time we invoke it,
we're going to get s bound to Jackie,
because Jackie is the value that is passed in.
So now, s is bound to Jackie,
and we'll get Jackie is greeting appearing in the output window. "Hello Jackie."
Often we'll refer to the parameter values as inputs.
Don't confuse that with the input function.
Remember, the input function asks the user to type in a value.
Here, we're talking about a value that is passed into a function as an input to it.
So, this Jackie, it's an input to
the Hello2 function or sometimes we'll call it an input parameter or a parameter value.
A function can take more than one input parameter.
Here, the function hello3 has two formal parameters;
s and n. We're going to refer to both of those parameters as we refer to on line two,
and we refer to on line three.
Remember that star when applied to
strings means to repeatedly concatenate the string together.
So, the greeting is going to be something like "Hello Wei" or "Hello
Kitty" and we're going to concatenate that together to itself a bunch of time,
so it just repeats that string n times.
Let's step through that code.
We create the string,
or we create the function rather, and then we call it.
The first time we call it,
we are passing in two parameter values, Wei and four.
The very first parameter value automatically
gets matched with the first formal parameter name.
So, we get s bound to Wei.
The second value goes to the second parameter name.
So, n gets the value four.
We call this positional parameter passing.
The first parameter value goes with the first parameter name.
When we execute on line two,
we'll get a value for greeting,
that's "Hello Wei" and on line three,
we will print that out a bunch of times,
and you can see down here we have said Hello Wei four times.
The next time we execute this,
we have s is bound to an empty string and n is one,
so our greeting is just going to be Hello with a couple of spaces.
We're going to print that out one time and it looks
just like Hello on the output because we can't see the spaces.
The last time we invoke this,
we've got Kitty and 11 is our two parameter values,
two inputs and so we get Hello Kitty,
Hello Kitty, Hello Kitty, 11 times.
In summary, formal parameters sometimes called parameter names or input parameters,
those are inside the parentheses in the function definitions.
So, s and n are formal parameters.
Then, we have parameter values or
sometimes they're called arguments or actual parameters.
Those go inside the parenthesis on the function invocation.
They get matched up positionally,
first parameter value with first parameter name,
second parameter value with second parameter name.
The values are bound to those parameter names at the beginning of
the function execution with this behind the scenes assignment.
It's as if we have s equals Wei and n equals four,
and that those assignment statements are executing behind
the scenes at the beginning of the invocation of hello3.
I'll see you next time for functions that produce outputs by a return values.